package com.xq.shop.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.springframework.stereotype.Repository;

import com.xq.shop.dao.IProductDAO;
import com.xq.shop.po.Product;
import com.xq.shop.po.ProductParam;
import com.xq.shop.util.HibernateUtil;
import com.xq.shop.vo.SearchObject;
import com.xq.shop.vo.SearchValue;

@Repository
public class ProductDAO extends BaseDAO<Product> implements IProductDAO {
	
	public int list(List<Map<String, Object>> param){
		
		String hql = " select product_id from ProductParam where ";

		List<Object> list = new ArrayList<Object>();
		for(Map<String, Object> map : param){
			
			Object param_id = map.get("param_id");
//			String type = map.get("type").toString();
			Object value = map.get("value");
			
			hql += "param_id=? and value=? or ";
			
			list.add(param_id);
			list.add(value);
		}
		hql = hql.substring(0, hql.length() - 3);
		hql += " group by product_id";
		System.out.println(hql);
		List result = HibernateUtil.getTemplate().find(hql, list.toArray());
		
		SearchObject search = new SearchObject();
		SearchValue c = new SearchValue();
		c.setName("id");
		c.setPattern(SearchValue.Pattern.IN);
		c.setValue(result);
		search.getCondition().add(c);
		List<Product> rl = HibernateUtil.getPageObject(Product.class, search);
		return rl.size();
	}
}
